package database

import (
	"api/utils"
	"fmt"
	"sync"

	_ "github.com/go-sql-driver/mysql"
	_ "sync"

	"github.com/jinzhu/gorm"
)

/**
 * @Description
 * @Author huan.wang
 * @Date 2021/5/5 11:12 下午
 **/


var db *gorm.DB
var once sync.Once



func GetDB() *gorm.DB{
	once.Do(func() {
		var err error
		var constr string
		constr = fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", utils.GetSettings().DBUser, utils.GetSettings().DBPass, utils.GetSettings().DBHost, utils.GetSettings().DBPort, utils.GetSettings().DBName)
		db, err = gorm.Open(utils.GetSettings().DB, constr)
		if err != nil {
			panic("数据库连接失败")
		}
	})
	return db
}

//// Migrate 初始化
//func  Migrate() {
//	db.AutoMigrate(&model.UserModel{})
//	db.AutoMigrate(&model.DeviceModel{})
//	db.AutoMigrate(&model.UserDeviceModel{})
//}